-- Dingo  (Nespouštět)
-- WMS: Cilove doklady davky -> Doklady davky WMS  (Nespouštět)
<g>
	<p>
	insert into a536721u11011 (u, ic, ts0, useric, useru, remotekey, langstate, u_a, ic_a, u_b, ic_b)
	select src.u, src.ic, getdate(), 1105, 0, src.remotekey, src.langstate, src.u_a, src.ic_a, src.u_b, src.ic_b
	</p>
	<a>
	insert into a536721u11011 (u, ic, ts0, useric, useru, remotekey, langstate, u_a, ic_a, u_b, ic_b, sstate, sstate_a, sstate_b)
	select src.u, src.ic, getdate(), 1105, 0, src.remotekey, src.langstate, src.u_a, src.ic_a, src.u_b, src.ic_b, 0, 0, 0
	</a>
	from
	  a288191u11011 src
	  left outer join a536721u11011 dst on src.ic = dst.ic and src.u = dst.u
	where
	  dst.ic is null
</g>

-- WMS: Zdrojove doklady davky -> Doklady davky WMS
<g>
	<p>
	insert into a536721u11011 (u, ic, ts0, useric, useru, remotekey, langstate, u_a, ic_a, u_b, ic_b)
	select src.u, src.ic, getdate(), 1105, 0, src.remotekey, src.langstate, src.u_a, src.ic_a, src.u_b, src.ic_b
	</p>
	<a>
	insert into a536721u11011 (u, ic, ts0, useric, useru, remotekey, langstate, u_a, ic_a, u_b, ic_b, sstate, sstate_a, sstate_b)
	select src.u, src.ic, getdate(), 1105, 0, src.remotekey, src.langstate, src.u_a, src.ic_a, src.u_b, src.ic_b, 0, 0, 0
	</a>
	from
	  a252171u11011 src
	  left outer join a536721u11011 dst on src.ic_a = dst.ic_a and src.u_a = dst.u_a and src.ic_b = dst.ic_b and src.u_b = dst.u_b
	where
	  dst.ic is null
</g>

-- WMS: "Hlavni doklad" Zdrojove doklady davky -> Doklady davky WMS
<g>
	update dst set useric = 1105, useru = 0, ts0 = getdate(), a536722u11011 = a285687u11011
	from
	  a252171u11011 src
	  inner join a536721u11011 dst on src.ic_a = dst.ic_a and src.u_a = dst.u_a and src.ic_a = dst.ic_b and src.u_b = dst.u_b and a536722u11011 is null and a285687u11011 is not null
</g>

-- WMS: "Hlavni doklad" pro cilovy doklad
<g>
	update dst set useric = 1105, useru = 0, ts0 = getdate(), a536722u11011 = 1
	from
	  a139317u11011 src
	  inner join a536721u11011 dst on src.ic = dst.ic and src.u = dst.u
	  left outer join a536721u11011 batch on src.ic_a = batch.ic_a and src.u_a = batch.u_a and batch.a536722u11011 = 1
	where
	  batch.ic is null and batch.u is null
</g>

-- WMS: odstraneni nepotrebne vazby "Zdrojovy doklad davky"
<g>
	delete src
	from
	  a139246u11011 src
	  left outer join a536721u11011 dst on src.ic = dst.ic and src.u = dst.u
	where
	  dst.ic is null and dst.u is null

	delete src
	from
	  a252171u11011 src
	  left outer join a536721u11011 dst on src.ic = dst.ic and src.u = dst.u
	where
	  dst.ic is null and dst.u is null
</g>
